package com.wss.common.view.scan;

import com.bingoogolapple.qrcode.core.QRCodeView;
import com.bingoogolapple.qrcode.zbar.ZBarView;
import com.bingoogolapple.qrcode.zxing.ZXingView;
import com.orhanobut.logger.Logger;
import com.wss.common.base.BaseAbility;
import com.wss.common.base.ResourceTable;
import com.wss.common.bean.Event;
import com.wss.common.constants.EventAction;
import com.wss.common.utils.EventBusUtils;
import com.wss.common.utils.PermissionsUtils;
import com.wss.common.utils.ToastUtils;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;

import java.util.Arrays;

public class ScanAbility extends BaseAbility {

    private ZBarView qrCodeView;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().setTransparent(true);
        qrCodeView = (ZBarView) findComponentById(ResourceTable.Id_qr_code_view);
        initView();
    }

    private void initView() {

        PermissionsUtils.checkCamera(this)
                .subscribe(
                aBoolean -> {
                    if (aBoolean) {

                        qrCodeView.setDelegate(new QRCodeView.Delegate() {
                            @Override
                            public void onScanQRCodeSuccess(String result) {

                                System.out.println("==== 二维码扫描结果：" + result);
                                ToastUtils.show(result);
                                EventBusUtils.sendEvent(new Event<>(EventAction.SCAN_QR_CODE_RESULT, result));
                                terminateAbility();
                            }

                            @Override
                            public void onCameraAmbientBrightnessChanged(boolean isDark) {
                                // 这里是通过修改提示文案来展示环境是否过暗的状态，接入方也可以根据 isDark 的值来实现其他交互效果
                                String tipText = qrCodeView.getScanBoxView().getTipText();
                                String ambientBrightnessTip = "\n环境过暗，请打开闪光灯";
                                if (isDark) {
                                    if (!tipText.contains(ambientBrightnessTip)) {
                                        qrCodeView.getScanBoxView().setTipText(tipText + ambientBrightnessTip);
                                    }
                                } else {
                                    if (tipText.contains(ambientBrightnessTip)) {
                                        tipText = tipText.substring(0, tipText.indexOf(ambientBrightnessTip));
                                        qrCodeView.getScanBoxView().setTipText(tipText);
                                    }
                                }
                            }

                            @Override
                            public void onScanQRCodeOpenCameraError() {
                                ToastUtils.show(context, "打开相机出错");
                            }
                        });

                    } else {
                        terminateAbility();
                    }
                }
        );

        findComponentById(ResourceTable.Id_iv_back).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
            }
        });
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        if (qrCodeView != null) {
            qrCodeView.stopCallBack();
        }
    }

    @Override
    protected void onActive() {
        super.onActive();
        qrCodeView.startCamera();
        qrCodeView.startSpotAndShowRect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (qrCodeView != null) {
            qrCodeView.onDestroy();
        }
    }

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_scan;
    }

}
